const mongoose = require('mongoose')
const path = require('path')
// 导入配置文件
const { DBHOST, DBPORT, DBNAME } = require(path.join(process.cwd(), '/config/config'))

// 定义模型
const menuItemSchema = new mongoose.Schema({
  name: { type: String, required: true },
  path: { type: String, required: true },
  title: { type: String, required: true },
  requiresAuth: { type: Boolean, required: true },
  icon: { type: String, required: true },
  menuType: { type: String, required: true },
  pinTab: { type: Boolean, default: false },
  keepAlive: { type: Boolean, default: false },
  componentPath: { type: String },
  id: { type: Number, required: true },
  pid: { type: Number, default: null }
})

const MenuItem = mongoose.model('router', menuItemSchema)

// 连接字符串
const CONNECTION_STRING = `mongodb://admin:123654@${DBHOST}:${DBPORT}/${DBNAME}?authSource=admin`

// 连接数据库
async function connectToDatabase() {
  try {
    await mongoose.connect(CONNECTION_STRING)
    console.log('MongoDB connected!')
  } catch (error) {
    console.error('MongoDB connection error:', error)
  }
}

// 插入数据
async function insertMenuItems(menuItems) {
  try {
    const result = await MenuItem.insertMany(menuItems)
    console.log('Data inserted:', result)
  } catch (error) {
    console.error('Error inserting data:', error)
  }
}

// 主程序
async function main() {
  await connectToDatabase()

  const menuItems = [
    {
      name: 'documents',
      path: '/documents',
      title: '外链文档',
      requiresAuth: true,
      icon: 'vscode-icons:folder-type-composer-opened',
      menuType: 'dir',
      keepAlive: false,
      componentPath: null,
      id: 1,
      pid: null
    },
    {
      name: 'documentsWMTS',
      path: '/documents/wmts',
      title: 'WMTS',
      requiresAuth: true,
      icon: 'vscode-icons:folder-type-api-opened',
      menuType: 'page',
      keepAlive: false,
      componentPath: '/documents/wmts/index.vue',
      id: 2,
      pid: 1
    },
    {
      name: 'documentsWMS',
      path: '/documents/wms',
      title: 'WMS',
      requiresAuth: true,
      icon: 'vscode-icons:folder-type-aurelia-opened',
      menuType: 'page',
      keepAlive: false,
      componentPath: '/documents/wms/index.vue',
      id: 3,
      pid: 1
    },
    {
      name: 'documents_iClient',
      path: '/documents/iClient',
      title: 'iClient',
      requiresAuth: true,
      icon: 'vscode-icons:folder-type-client-opened',
      menuType: 'page',
      keepAlive: false,
      componentPath: '/documents/iClient/index.vue',
      id: 4,
      pid: 1
    },
    {
      name: 'documents_iClient3D',
      path: '/documents/iClient3D',
      title: 'iClient3D',
      requiresAuth: true,
      icon: 'vscode-icons:folder-type-bower-opened',
      menuType: 'page',
      keepAlive: false,
      componentPath: '/documents/iClient3D/index.vue',
      id: 5,
      pid: 1
    },
    {
      name: 'documents_CesiumJS',
      path: '/documents/CesiumJS',
      title: 'CesiumJS',
      requiresAuth: true,
      icon: 'vscode-icons:folder-type-chef-opened',
      menuType: 'page',
      keepAlive: false,
      componentPath: '/documents/CesiumJS/index.vue',
      id: 6,
      pid: 1
    },
    {
      name: 'wms',
      path: '/wms',
      title: 'wms',
      requiresAuth: true,
      icon: 'gis:wms',
      menuType: 'dir',
      keepAlive: false,
      componentPath: null,
      id: 7,
      pid: null
    },
    {
      name: 'leaflet_wms',
      path: '/wms/leaflet_wms',
      title: 'leaflet',
      requiresAuth: true,
      icon: 'el:leaf',
      menuType: 'page',
      keepAlive: true,
      componentPath: '/wms/leaflet/index.vue',
      id: 8,
      pid: 7
    },
    {
      name: 'openlayers_wms',
      path: '/wms/openlayers_wms',
      title: 'openlayers',
      requiresAuth: true,
      icon: 'fluent-mdl2:map-layers',
      menuType: 'page',
      keepAlive: true,
      componentPath: '/wms/openlayers/index.vue',
      id: 9,
      pid: 7
    },
    {
      name: 'cesium_wms',
      path: '/wms/cesium_wms',
      title: 'cesium',
      requiresAuth: true,
      icon: 'icomoon-free:earth',
      menuType: 'page',
      keepAlive: true,
      componentPath: '/wms/cesium/index.vue',
      id: 10,
      pid: 7
    },
    {
      name: 'webgl_wms',
      path: '/wms/webgl_wms',
      title: 'webgl',
      requiresAuth: true,
      icon: 'gis:earth-network-o',
      menuType: 'page',
      keepAlive: true,
      componentPath: '/wms/webgl/index.vue',
      id: 11,
      pid: 7
    },
    {
      name: 'wmts',
      path: '/wmts',
      title: 'wmts',
      requiresAuth: true,
      icon: 'gis:wmts',
      menuType: 'dir',
      keepAlive: false,
      componentPath: null,
      id: 12,
      pid: null
    },
    {
      name: 'leaflet_wmts',
      path: '/wmts/leaflet_wmts',
      title: 'leaflet',
      requiresAuth: true,
      icon: 'el:leaf',
      menuType: 'page',
      keepAlive: true,
      componentPath: '/wmts/leaflet/index.vue',
      id: 13,
      pid: 12
    },
    {
      name: 'openlayers_wmts',
      path: '/wmts/openlayers_wmts',
      title: 'openlayers',
      requiresAuth: true,
      icon: 'fluent-mdl2:map-layers',
      menuType: 'page',
      keepAlive: true,
      componentPath: '/wmts/openlayers/index.vue',
      id: 14,
      pid: 12
    },
    {
      name: 'cesium_wmts',
      path: '/wmts/cesium_wmts',
      title: 'cesium',
      requiresAuth: true,
      icon: 'icomoon-free:earth',
      menuType: 'page',
      keepAlive: true,
      componentPath: '/wmts/cesium/index.vue',
      id: 15,
      pid: 12
    },
    {
      name: 'webgl_wmts',
      path: '/wmts/webgl_wmts',
      title: 'webgl',
      requiresAuth: true,
      icon: 'gis:earth-network-o',
      menuType: 'page',
      keepAlive: true,
      componentPath: '/wmts/webgl/index.vue',
      id: 16,
      pid: 12
    },
    {
      name: 'tools',
      path: '/tools',
      title: '解析能力文档',
      requiresAuth: true,
      icon: 'unjs:db0',
      menuType: 'dir',
      keepAlive: false,
      componentPath: null,
      id: 17,
      pid: null
    },
    {
      name: 'wms_tool',
      path: '/tools/wms_tool',
      title: 'wms_tool',
      requiresAuth: true,
      icon: 'gis:wms',
      menuType: 'page',
      keepAlive: true,
      componentPath: '/tools/wms_tool/index.vue',
      id: 18,
      pid: 17
    },
    {
      name: 'wmts_tool',
      path: '/tools/wmts_tool',
      title: 'wmts_tool',
      requiresAuth: true,
      icon: 'gis:wmts',
      menuType: 'page',
      keepAlive: true,
      componentPath: '/tools/wmts_tool/index.vue',
      id: 19,
      pid: 17
    },
    {
      name: 'user_manger',
      path: '/user_manger',
      title: '用户管理',
      requiresAuth: true,
      icon: 'icon-park-outline:every-user',
      menuType: 'page',
      keepAlive: false,
      componentPath: '/userManger/index.vue',
      id: 20,
      pid: null
    },
    {
      name: 'user_center',
      path: '/user_center',
      title: '个人中心',
      requiresAuth: true,
      icon: 'carbon:user-avatar-filled-alt',
      menuType: 'page',
      keepAlive: false,
      componentPath: '/userCenter/index.vue',
      id: 21,
      pid: null
    },
    {
      name: 'about',
      path: '/about',
      title: '关于',
      requiresAuth: true,
      icon: 'fxemoji:sunglasses',
      menuType: 'page',
      pinTab: true,
      keepAlive: false,
      componentPath: '/about/index.vue',
      id: 22,
      pid: null
    }
  ]

  await insertMenuItems(menuItems)

  mongoose.connection.close() // 关闭连接
}

main()
